<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

	protected $is_connected;

	public function __construct(){
		parent::__construct();
		$this->is_connected =  !$this->session->userdata('id_admin') == null;
		$this->load->view('admin/header', array('is_connected'=>$this->is_connected));
	}

	public function index($message = '')
	{	
		if(!$this->is_connected)
			$this->load->view('admin/index', array('message'=>$message));
		else{
			$this->load->model('news_model');
			$this->load->view('admin/news', array('news'=>$this->news_model->get_news()));
		}
		$this->load->view('footer');
	}

	public function inscriptions()
	{	
		if(!$this->is_connected)
			redirect('admin');
		$this->load->model('inscrits_model');
		$this->load->view('admin/inscriptions', array('progress'=>$this->inscrits_model->get_inscrits(0), 'validated'=>$this->inscrits_model->get_inscrits(1)));
		$this->load->view('footer');
	}

	public function valid_inscription(){
		if(!$this->is_connected)
			redirect('admin');
		$this->form_validation->set_rules('num_cagnard','N° de cagnard','numeric|trim|required|xss_clean');
		if($this->form_validation->run()){
			$message = 'Bonjour,<br/>
Nous avons bien reçu votre paiement pour le Crédit Agricole Tour 2013.<br/>
Voici le N° de Cagnard qui vous a été attribué : <b>'.$this->input->post('num_cagnard').'</b><br/>
Pour toute question, n\'hésitez pas à nous contacter : contact@cdv56.com<br/><br/>
Merci et à très bientôt.<br/><br/>
Cordialement<br/>
Comité Départemental de Voile du Morbihan';
			$this->load->library('email');
			$this->email->from('contact@cdv56.com', 'Comité Départemental de Voile du Morbihan');
			$this->email->to($this->input->post('email'));
			$this->email->subject('Votre inscription au Crédit Agricole Tour 2013 a été validée');
			$this->email->message($message);
			if($this->email->send()){
				$this->load->model('inscrits_model');
				$this->inscrits_model->valid($this->input->post('id_inscrit'), $this->input->post('num_cagnard'));
				redirect('admin/inscriptions');
			}
			else{
				redirect('admin/inscriptions_error');
			}
		}

		$this->inscriptions();
	}

	public function inscriptions_error(){
		if(!$this->is_connected)
			redirect('admin');
		$this->load->view('admin/inscriptions_error');
		$this->load->view('footer');
	}

	public function cancel_inscription(){
		if(!$this->is_connected)
			redirect('admin');
		$message = 'Bonjour,<br/>
Votre inscription au Crédit Agricole Tour 2013 a bien été annulée.<br/>
Si vous avez déjà payé vous inscription, vous recevrez très prochainement les chèques que vous avez envoyés lors de votre inscription.<br/>
Pour toute question, n\'hésitez pas à nous contacter : contact@cdv56.com<br/><br/>
Merci et à très bientôt.<br/><br/>
Cordialement<br/>
Comité Départemental de Voile du Morbihan';
			$this->load->library('email');
			$this->email->from('contact@cdv56.com', 'Comité Départemental de Voile du Morbihan');
			$this->email->to($this->input->post('email'));
			$this->email->subject('Annulation de votre inscription au Crédit Agricole Tour 2013');
			$this->email->message($message);
			if($this->email->send()){
				$this->load->model('inscrits_model');
				$this->inscrits_model->remove($this->input->post('id_inscrit'));
				redirect('admin/inscriptions');
			}
			else{
				redirect('admin/cancel_error');
			}
		
	}

	public function cancel_error(){
		if(!$this->is_connected)
			redirect('admin');
		$this->load->view('admin/cancel_error');
		$this->load->view('footer');
	}

	public function connection(){
		$this->load->model('admin_model');
		$this->form_validation->set_rules('login','Identifiant','trim|required|xss_clean');
		$this->form_validation->set_rules('password','Mot de passe','trim|required|xss_clean');
		if($this->form_validation->run()){
			$connection = $this->admin_model->connect($this->input->post('login'), $this->input->post('password'));
			if($connection != null){
				$this->session->set_userdata($connection);
				redirect('admin/index');
			}
			$this->index('Le couple identifiant/mot de passe est incorrect');
		}
		else
			$this->index();
	}

	public function deconnection(){
		if(!$this->is_connected)
			redirect('admin');
		$this->session->sess_destroy();
		redirect();
	}

	public function new_news(){
		if(!$this->is_connected)
			redirect('admin');
		$this->load->model('news_model');
		$this->form_validation->set_rules('title','Titre','trim|required|xss_clean');
		$this->form_validation->set_rules('news_content','Contenu','trim|required|xss_clean');
		if($this->form_validation->run()){
			$this->news_model->insert($this->input->post('title'), $this->input->post('news_content'));
			redirect('admin/index');
		}
		else
			$this->index();
	}

	public function modif_news(){
		if(!$this->is_connected)
			redirect('admin');
		$this->load->model('news_model');
		$theNews = $this->news_model->get_news_by_id($this->uri->segment(3));
		if($theNews == null)
			redirect('admin/index');
		$this->load->view('admin/modif_news', array('theNews'=>$theNews, 'news'=>$this->news_model->get_news()));
		$this->load->view('footer');
	}

	public function update_news(){
		if(!$this->is_connected)
			redirect('admin');
		$this->load->model('news_model');
		$this->form_validation->set_rules('title','Titre','trim|required|xss_clean');
		$this->form_validation->set_rules('news_content','Contenu','trim|required|xss_clean');
		if($this->form_validation->run()){
			$this->news_model->update_news($this->input->post('id_news'), $this->input->post('title'), $this->input->post('news_content'));
			redirect('admin/index');
		}
		else
			$this->update_news();
	}

	public function remove_news(){
		if(!$this->is_connected)
			redirect('admin');
		$this->load->model('news_model');
		$this->news_model->remove_news($this->uri->segment(3));
		redirect('admin/index');
	}

	public function photos(){
		if(!$this->is_connected)
			redirect('admin');
		$this->load->model('photos_model');
		$this->load->view('admin/photos', array('error'=>'', 'photos'=>$this->photos_model->get_photos()));
		$this->load->view('footer');
	}

	public function upload_photo(){
		if(!$this->is_connected)
			redirect('admin');
		$config['upload_path'] = './assets/images/upload/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '2048';
		$config['encrypt_name']	= TRUE;
		$this->load->library('upload', $config);
		$this->form_validation->set_rules('title','Titre','trim|required|xss_clean');
		if($this->form_validation->run()){
			if($this->upload->do_upload('photo')){
				$this->load->model('photos_model');
				$data = $this->upload->data();
				$this->photos_model->insert($this->input->post('title'), $config['upload_path'].$data['file_name']);
        		$config2['source_image'] = $config['upload_path'].$data['file_name'];
       			$config2['maintain_ratio'] = TRUE;
       			$config2['width'] = 800;
       			$config2['height'] = 600;
        		$this->load->library('image_lib', $config2);
        		if($this->image_lib->resize())
					redirect('admin/photos');
			}
		}
		$this->load->view('admin/photos', array('error'=>$this->upload->display_errors()));
		$this->load->view('footer');
	}

	public function remove_photo(){
		if(!$this->is_connected)
			redirect('admin');
		$this->load->model('photos_model');
		$this->photos_model->remove_photo($this->uri->segment(3));
		redirect('admin/photos');
	}

	public function details_inscription(){
		if(!$this->is_connected)
			redirect('admin');
		$this->load->model('inscrits_model');
		$inscrit = $this->inscrits_model->get_inscrit_by_id($this->uri->segment(3));
		if($inscrit == null)
			redirect('admin/index');
		$this->load->view('admin/details_inscription', array('inscrit'=>$inscrit));
		$this->load->view('footer');
	}

	public function switch_categorie(){
		if(!$this->is_connected)
			redirect('admin');
		$this->load->model('inscrits_model');
		$this->inscrits_model->switch_categorie($this->uri->segment(3));
		redirect('admin/inscriptions');
	}

	public function resultats(){
		if(!$this->is_connected)
			redirect('admin');
		$this->load->model('resultats_model');
		$this->load->model('inscrits_model');
		$course = $this->uri->segment(3);
		$course = ($course == null)? 1 : $course;
		$resultats = $this->resultats_model->get_resultats_by_course($course);
		$res = array();
		foreach($resultats as $r){
			$res[$r['id_part']] = $r['position'];
		}
		$this->load->view('admin/resultats', array(
			'inscrits1'=>$this->inscrits_model->get_inscrits(1,1),
			'inscrits2'=>$this->inscrits_model->get_inscrits(1,2),
			'resultats'=>$res,
			'regate'=>$course));
		$this->load->view('footer');
	}

	public function valid_resultats(){
		if(!$this->is_connected)
			redirect('admin');
		$this->load->model('resultats_model');
		$this->load->model('inscrits_model');
		$course = $this->input->post('course');
		$this->resultats_model->remove_resultats($course);
		$parts = $this->input->post('parts');
		$positions = $this->input->post('positions');
		foreach($parts as $i=>$p){
			$this->resultats_model->insert($course, $p, $positions[$i]);
		}
		redirect('admin/resultats/'.$course);
	}
}
